package com.romadan.core.nonlinearanalys.fet.angelov;

import com.romadan.core.wolfram.WolframPerfomer;
import com.wolfram.jlink.Expr;

public class WolframTransistorModel {

	private WolframPerfomer wp = new WolframPerfomer();

	public WolframTransistorModel() {
		wp.evaluateExpr("getModelAngelov:=Module[{psi, Vpk},Vpk=Vpk0+w0*Vds;psi=P1(Vgs-Vpk)+P2 (Vgs-Vpk)^2+P3 (Vgs-Vpk)^3;Ipk*(1+Tanh[psi])(1+lymda*Vds)*Tanh[alfa*Vds]]");
		wp.evaluateExpr("getModelAngelovParameters:=Module[{},{Vpk0,w0,P1,P2,P3,Ipk,lymda,alfa}]");
		wp.evaluateExpr("getParamForAngelov[dataList_,VdsValue_,podstavnParam_,peremenParam_]:=Module[{model},model=getModelAngelov/.{Vds->VdsValue}/.podstavnParam;(*per=Complement[getModelAngelovParameters, podstavnParam];*)FindFit[dataList, model, peremenParam, Vgs]]");
		wp.evaluateExpr("hello:=Module[{},\"Package connected!\"]");
	}

	public Expr getModelAngelov() {
		return wp.evaluateExpr("getModelAngelov[" + "]");
	}

	public Expr getModelAngelovParameters() {
		return wp.evaluateExpr("getModelAngelovParameters[" + "]");
	}

	public Expr getParamForAngelov(Expr dataList, Expr VdsValue,
			Expr podstavnParam, Expr peremenParam) {
		return wp.evaluateExpr("getParamForAngelov[" + dataList.toString()
				+ ", " + VdsValue.toString() + ", " + podstavnParam.toString()
				+ ", " + peremenParam.toString() + "]");
	}

	public Expr hello() {
		return wp.evaluateExpr("hello[" + "]");
	}

	public Expr getParamForAngelov(Expr dataList, Expr VdsValue) {
		// TODO Plug!!!!
		WolframPerfomer p = new WolframPerfomer();
		Expr podstavnParam = p.evaluateExpr("{}");
		Expr peremenParam = p
				.evaluateExpr("{Vpk0, w0, P1, P2, P3, Ipk, lymda, alfa}");
		p.close();
		return wp.evaluateExpr("getParamForAngelov[" + dataList.toString()
				+ ", " + VdsValue.toString() + ", " + podstavnParam.toString()
				+ ", " + peremenParam.toString() + "]");
	}

	public void close() {
		wp.close();
	}

}
